<?php

namespace app\yy\services;

use app\user\model\Message as MessageModel;

class BaseService
{
    public $web_url = '/yy_image.php';
    public $oss_url = 'https://images.yiyangssl.com';

    /**
     * @param $list
     * @return array
     */
    function categoryFormat($list): array
    {
        $categoryList = $sort = [];
        foreach ($list as &$cate) {
            $title = explode('/', $cate);
            $cateItem['title'] = $cate;
            $cateItem['name'] = array_pop($title);
            $cateItem['parent_sku'] = implode("/", $title);
            $cateItem['sort'] = $sort[] = count($title);
            $categoryList[] = $cateItem;
        }
        array_multisort($sort, $categoryList);
        return $categoryList;
    }


    public function getOptionPrice($options2, $price): array
    {
        // 判断是否是多变值选项
        $ends = [];
        foreach ($options2 as &$option) {
            $end = array_pop($option);
            $ends[] = $end;
            $option[] = $end - $price;
        }

        if (count(array_unique($ends)) < 2) {
            return [];
        }
        return $this->ax($options2);
    }


    public function ax($options): array
    {
        $array_key_item = [];
        $count = count($options[0]);
        if ($count > 3) {
            $item = array_slice($options[0], 1, -1);
            $item_1 = implode('_', $item);
            $array_key_item[$item_1] = $item;
        } else {
            $item_1 = $options[0][1];
        }
        $array_key[(string)($options[0][0])] = 0;
        $array_key[(string)$item_1] = $options[0][$count - 1];

        array_shift($options);
        foreach ($options as $key => $option) {
            $count = count($option);
            if ($count == 2) {
                $array_key[(string)$option[0]] = end($option);
            } else {
                if ($count > 3) {
                    $item = array_slice($option, 1, -1);
                    $item_1 = implode('_', $item);
                    $array_key_item[$item_1] = $item;
                } else {
                    $item_1 = $option[1];
                }
                if (isset($array_key[(string)$option[0]])) {
                    $array_key[(string)$item_1] = end($option) - $array_key[(string)$option[0]];
                } else {
                    $array_key[(string)$option[0]] = end($option) - $array_key[(string)$item_1];
                }
            }
        }
        if ($array_key_item) {
            foreach ($array_key_item as $k => &$value) {
                $value[] = $array_key[$k];
            }
            return array_merge($array_key, $this->ax(array_values($array_key_item)));
        }
        return $array_key;
    }

    public function message($data)
    {
        $uids = [ '2','4'];
        foreach ($uids as $uid) {
            $list[] = [
                'uid_receive' => $uid,
                'uid_send'    => 1,
                'type'        => $data['type'],
                'content'     => $data['content'],
            ];
        }
        $MessageModel = new MessageModel;
        $MessageModel->saveAll($list);
        return true;
    }

    /**
     * 发送https/http请求  正常/异步
     * @param string $url 请求地址
     * @param array $params http请求数据, 数据类型必须是Array
     * @param string $method http方法(GET POST PUT DELETE)
     * @param array $header http请求头
     * @param int $type 请求类型 0正常，1异步
     * @param int $duration 请求时间
     * @return string|bool
     */
    function request_curl($url, $method = "POST", $duration = 100000, $params = [], $header = [], $type = 0)
    {
        //检查地址是否为空
        if (empty($url)) {
            return false;
        }
        //控制请求方法范围
        $httpMethod = array('GET', 'POST', 'PUT', 'DELETE');
        $method = strtoupper($method);
        if (!in_array($method, $httpMethod)) {
            return false;
        }
        //请求头初始化
        $request_headers = array();
        $User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
        $request_headers[] = 'User-Agent: ' . $User_Agent;
        if ($header) {
            foreach ($header as $v) {
                $request_headers[] = $v;
            }
        }

        $request_headers[] = 'Accept: text/html,application/json,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
        switch ($method) {
            case "POST":
                $request_headers[] = "X-HTTP-Method-Override: POST";
                break;
            case "PUT":
                $request_headers[] = "X-HTTP-Method-Override: PUT";
                break;
            case "DELETE":
                $request_headers[] = "X-HTTP-Method-Override: DELETE";
                break;
            default:
        }

        //格式化发送数据
        if ($params) {
            if ($method == 'GET') {
                //GET请求
                $url = $url . '?' . http_build_query($params);
            } else {
                if (!$type) {
                    $params = json_encode($params, JSON_UNESCAPED_UNICODE);
                }
            }
        }

        //发送http请求
        $ch = curl_init();
        if ($type) {
            curl_setopt($ch, CURLOPT_TIMEOUT, 1);//设置超时时间为1秒,超过1秒则关闭连接
            #curl_setopt($ch, CURLOPT_NOSIGNAL, 1);     //注意，毫秒超时一定要设置这个
            #curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); //超时毫秒，cURL 7.16.2中被加入。从PHP 5.2.3起可使用
        } else {
            curl_setopt($ch, CURLOPT_TIMEOUT_MS, $duration); // 最大执行时间
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $duration); // 在尝试连接时等待的秒数
        }
        curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($ch, CURLOPT_HEADER, 0);// 设置是否显示返回头信息  1返回 0不返回
        curl_setopt($ch, CURLOPT_NOBODY, 0); //不想在输出中包含body部分，设置这个选项为一个非零值
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https 不进行ssl验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);//设置头信息
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
        if ($method != 'GET') {
            if ($method != 'POST') {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            }
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        }

        //发送请求获取返回响应
        $result['data'] = curl_exec($ch);
        $result['httpCode'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if (strlen(curl_error($ch)) > 1) {
            $result = false;
        }
        curl_close($ch);
        return $result['data'];
    }

}